(function($){
   $(document).ready(function(){
      $('head').append('<style>.results-pdf,.results-youtube,.results-doc{cursor:pointer}.mm-pdf-viewer{padding:5px;}.mm-download{padding:20px;background:#367CF1;cursor:pointer;text-align:center;font-size:18px;font-weight:bold;}.mm-download:hover{background:#0D50BF;}.mm-youtube{padding:5px;}.mm-goto{padding:20px;background:#367CF1;cursor:pointer;text-align:center;font-size:18px;font-weight:bold;}.mm-goto:hover{background:#0D50BF;}a.playbtn,a.playingmp3,a.embedbtn,.showingembedmp3{color:#0000EE !important;margin-right:10px !important}a.dlbtn{color:#008000 !important;margin-right:10px !important}</style>');
      
      /* Results for PDF */
      $('.results-pdf a').click(function(){
         return false;
      });
      
      $('.results-pdf').click(function(){
         var pdf_url = $(this).children('div.desc').children('a').attr('href');
         $('.mm-pdf-viewer').remove();
         $(this).after('<div class="mm-pdf-viewer" id="mm-to-'+$(this).index()+'"></div>');
         $('.mm-pdf-viewer').append('<iframe src="http://docs.google.com/viewer?embedded=true&url='+pdf_url+'" style="width:100%;height:425px"></iframe>');
         $('.mm-pdf-viewer').append('<div class="mm-download" onclick="window.open(\''+pdf_url+'\');">Click here to download</div>');
         $('body,html').animate({scrollTop: $('.mm-pdf-viewer').prev().offset().top}, 800);
      });
      
      /* Results for DOC */
      $('.results-doc a').click(function(){
         return false;
      });
      $('.results-doc').click(function(){
         var pdf_url = $(this).children('div.desc').children('a').attr('href');
         $('.mm-doc-viewer').remove();
         $(this).after('<div class="mm-doc-viewer" id="mm-to-'+$(this).index()+'"></div>');
         $('.mm-doc-viewer').append('<iframe src="http://docs.google.com/viewer?embedded=true&url='+pdf_url+'" style="width:100%;height:425px"></iframe>');
         $('.mm-doc-viewer').append('<div class="mm-download" onclick="window.open(\''+pdf_url+'\');">Click here to download</div>');
         $('body,html').animate({scrollTop: $('.mm-doc-viewer').prev().offset().top}, 800);
      });
      
      /* Results for YouTube */
      $('.results-youtube a').click(function(){
         return false;
      });
      $('.results-youtube').click(function(){
         var youtube_url = $(this).children('div.desc').children('a').attr('href');
         var video_id = youtube_url.split('v=')[1];
         var ampersandPosition = video_id.indexOf('&');
         if(ampersandPosition != -1) {
            video_id = video_id.substring(0, ampersandPosition);
         }
         $('.mm-youtube').remove();
         $(this).after('<div class="mm-youtube" id="mm-to-'+$(this).index()+'"></div>');
         $('.mm-youtube').append('<iframe width="420" height="315" src="http://www.youtube.com/embed/'+video_id+'?wmode=opaque" frameborder="0" allowfullscreen></iframe>');
         $('body,html').animate({scrollTop: $('.mm-youtube').prev().offset().top}, 800);
      });
      
      /* Results for MP3 */
      $('a.playbtn').live('click',function(e){
         e.preventDefault();
         $(this).hide();
         $(this).after('<a href="javascript:;" class="playingmp3">Stop</a>');
         $(this).parent('.playtool').after('<div class="mp3player" style="margin:3px 0"><embed type="application/x-shockwave-flash" src="http://www.4shared.com/flash/player.swf?ver=9051" style="" id="ply" name="ply" quality="high" allowscriptaccess="always" allowfullscreen="false" wmode="opaque" flashvars="file='+$(this).attr('rel')+'&amp;volume=50&amp;autostart=true" height="20" width="295"></div>');
      });
      $('a.playingmp3').live('click',function(e){
         e.preventDefault();
         $(this).prev($('a.playbtn')).show();
         $(this).parent('div.playtool').next().remove();
         $(this).remove();
      });
      $('a.dlbtn').click(function(){
         window.open($(this).attr('rel'));
      })
      
      $('a.embedbtn').live('click',function(e){
         e.preventDefault();
         var title = $(this).parent('.playtool').prev().text().replace('.mp3', '');
         $(this).hide();
         $(this).after('<a href="javascript:;" class="showingembedmp3">Hide Embed</a>');
         $(this).parent('.playtool').after('<div class="mp3embedder" style="margin:3px 0;"><input style="padding:3px;font-family:13px;" value="'+title+'&lt;br&gt;&lt;embed type=&quot;application/x-shockwave-flash&quot; src=&quot;http://www.4shared.com/flash/player.swf?ver=9051&quot; style=&quot;&quot; id=&quot;ply&quot; name=&quot;ply&quot; quality=&quot;high&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;false&quot; wmode=&quot;opaque&quot; flashvars=&quot;file='+$(this).attr('rel')+'&amp;amp;volume=50&quot; height=&quot;20&quot; width=&quot;200&quot;&gt;&lt;br&gt;Powered by &lt;a href=&quot;'+document.location.origin+'&quot;&gt;'+document.location.host+'&lt;/a&gt;" /></div>');
      });
      
      $('a.showingembedmp3').live('click',function(e){
         e.preventDefault();
         $(this).prev($('a.embedbtn')).show();
         $(this).parent('div.playtool').next().remove();
         $(this).remove();
      });
   });
})(jQuery);